package com.ruoyi.web.controller.system;

import com.aliay.sdk.AlipayConfig;
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.ruoyi.back.domain.OrderDetail;
import com.ruoyi.back.service.IOrderDetailService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName AliPayController
 * @Description TODO
 * @Author luguanqing
 * @Date 2021/4/26 9:08
 * @Version 1.0
 */
@RestController
@RequestMapping("/alipay")
public class AliPayController {

    @Autowired
    private IOrderDetailService orderDetailService;

    @PostMapping("/pay")
    private void alipayPay(@RequestBody OrderDetail orderDetail, HttpServletResponse response) throws Exception {
        //初始化AliPayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.APP_ID,
                AlipayConfig.APP_PRIVATE_KEY, "json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.sign_type);
        // 设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = orderDetail.getOrderNo();
        //付款金额，必填
        String total_amount = orderDetail.getTotalMoney();
        //订单名称，必填
        String subject = orderDetail.getSubject();
        //商品描述，可空
        String body = orderDetail.getNote();
        Map<String, Object> payMap = new HashMap<String, Object>(16);
        payMap.put("out_trade_no", out_trade_no);
        payMap.put("total_amount", total_amount);
        payMap.put("subject", subject);
        payMap.put("product_code", "FAST_INSTANT_TRADE_PAY");
        String json = JSON.toJSONString(payMap);
        alipayRequest.setBizContent(json);
        String result = null;
        orderDetail.setOrderStatus("USERPAYING");
        orderDetail.setId(StringUtils.replace(UUID.randomUUID().toString(), "-", ""));
        orderDetail.setPaymentWay("zfb");
        orderDetailService.insertOrderDetail(orderDetail);
        try {
            result = alipayClient.pageExecute(alipayRequest).getBody();
        } catch (Exception e) {
           e.printStackTrace();
        }
        //请求
        response.setContentType("text/html;charset=UTF-8");
        String head = "<html><head><meta http-equiv='Content-Type' content='text/html;charset=UTF-8'></head>";
        String bottom = "<body></body></html>";
        //输出
        response.getWriter().println(head + result + bottom);
    }

}
